<?php 
defined('IN_SITE') or exit('Access Denied');
@set_time_limit(600);
$html = load('html.class.php','html');
include MOD_ROOT.'template/template.func.php';

switch($action)
{
    case 'index':
        $filesize = $html->index();
	    showmessage('网站首页更新成功！<br />大小：'.sizecount($filesize));
		break;

    case 'category':
		if($dosubmit)
		{
			
			if(!isset($count))
			{
				$cids = array();
				
				// 如果选择的是所有栏目，则将所有栏目id(除了外部链接)列出来成为一个数组
				if(!isset($catids) || $catids[0] == 0) 
				{
					foreach($CATEGORY as $cid=>$v)
					{
						if($v['type'] <= 1) $catids[] = $cid;
					}
					
					// 移除键为0的元素，key为0的元素catid为0，不删除的话在下面的静态化中会报错
					unset($catids[0]);
				}
				
                foreach($catids as $k=>$id)
                {
					if($CATEGORY[$id]['type'] == 0)
					{
						$cids[] = $id;
						
						$catid = $id; global $catid;
						$html->category($id);
					}
					elseif($CATEGORY[$id]['type'] == 1)
					{
						// 解决单网页不能更新的问题 aming
						$cids[] = $id;  
						$html->category($id);
					}
                }
				
				if($cids)
				{
					cache_write('html_category_'.$_userid.'.php', $cids);
					$count = count($cids);
					
					$forward = urlencode($forward);
					showmessage('开始更新栏目页...', "?module=$module&action=$action&forward=$forward&pagesize=$pagesize&dosubmit=1&count=$count");
				}
				else
				{
					showmessage('更新完成！', "?module=$module&action=$action");
				}
            }
			else
			{
				$catids = cache_read('html_category_'.$_userid.'.php');
				
				$page = max(intval($page), 1);
				
				// 当第一次执行的时候，获取第一个catid，同时从数据里面删除
				if($page == 1)
				{
					// 删除数组中的第一个元素，并返回被删除元素的值
				    $catid = array_shift($catids);
					
					// 这里是生成静态化不是动态地址访问 没法获取参数catid 需要在这里获得并且GLOBAL
					global $catid;
					
					cache_write('html_category_'.$_userid.'.php', $catids);
					
					//$html->category($catid);
                }
				
				// 栏目名称
				$catname = $CATEGORY[$catid]['catname'];
				
				// 当是栏目页的时候
				if($CATEGORY[$catid]['child'] || $CATEGORY[$catid]['type'] == 1)
				{	
					$pages = 1;
					$html->category($catid);
				}
				// 当是列表页的时候
				else
				{
					// 第一次执行的时候开始算一下总数量，和总页数就不用每次都计算了加快执行速度
					if($page == 1)
					{
						// 获取栏目模板get标签里面的配置数组
						$getconfig = template_get_config($catid);
						
						// 如果没有获取到getconfig那说明模板里面没有get调取数据，那么不需要去获取列表的数据直接更新
						if($getconfig){
							
							// 每页信息条数
							$pagesize = $getconfig['rows'];
							
							// 将get标签里的sql语句的变量赋值
							eval("\$sql = \"$getconfig[sql]\";");
							
							//echo $sql;exit();
							
							$result = $db->select("$sql");
							$total = count($result);
							unset($result);
							
							$pages = ceil($total/$pagesize);
						}
						//$page = $page ? $page : 1;
						
					}
					
					
				}
				if($pages >= $page)
				{
					// 生成列表页分页的页面
					$html->category($catid, $page);
				
					$message = "正在更新 <font color='blue'>$catname</font> 栏目，共需更新 <font color='red'>$pages</font> 个网页<br />已更新 <font color='red'>{$page}</font> 个网页";
					
					$page++;
					$forward = url_par("catid=$catid&page=$page&pages=$pages&total=$total");
				}
				elseif($catids)
				{
					$message = "<font color='blue'>$catname</font> 栏目更新完成！";
					$forward = url_par("catid=0&page=0&pages=0&total=0");
				}
				else
				{
					cache_delete('html_category_'.$_userid.'.php');
					$message = "更新完成！";
					$forward = '?module=html&action=category';
				}
				showmessage($message, $forward);
			}
		}
		else
		{
			include ('html_category.tpl.php');
        }
		break;

    case 'show':
		if($dosubmit)
		{
			require_once MOD_ROOT.'attachment/attachment.class.php';
			require_once CACHE_MODEL_PATH.'content_output.class.php';
			require_once MOD_ROOT.'content/content.class.php';

			$attachment = new attachment($module, $catid);
			$c = new content();
			$coutput = new content_output();
			if($type == 'lastinput')
			{
				$offset = 0;
			}
			else
			{
				$page = max(intval($page), 1);
				$offset = $pagesize*($page-1);
			}
		    $where = ' WHERE status=99 ';
			$order = 'ASC';
			
			if(!isset($first) && is_array($catids) && $catids[0] > 0) 
			{
				cache_write('html_show_'.$_userid.'.php', $catids);
				$catids = implodeids($catids);
				$where .= " AND catid IN($catids) ";
				$first = 1;
			}
			elseif($first)
			{
				$catids = cache_read('html_show_'.$_userid.'.php');
				$catids = implodeids($catids);
				$where .= " AND catid IN($catids) ";
			}
			else
			{
				$first = 0;
			}

			if($type == 'lastinput' && $number)
			{
				$offset = 0;
				$pagesize = $number;
				$order = 'DESC';
			}
			elseif($type == 'date')
			{
				if($fromdate)
				{
					$fromtime = strtotime($fromdate.' 00:00:00');
					$where .= " AND `inputtime`>=$fromtime ";
				}
				if($todate)
				{
					$totime = strtotime($todate.' 23:59:59');
					$where .= " AND `inputtime`<=$totime ";
				}
			}
			elseif($type == 'id')
			{
				$fromid = intval($fromid);
				$toid = intval($toid);
				if($fromid) $where .= " AND `contentid`>=$fromid ";
				if($toid) $where .= " AND `contentid`<=$toid ";
			}
			if(!isset($total) && $type != 'lastinput')
			{
				$total = cache_count("SELECT COUNT(*) AS `count` FROM `".DB_PRE."content` $where");
				$pages = ceil($total/$pagesize);
				$start = 1;
			}
			$data = $db->select("SELECT `contentid` FROM `".DB_PRE."content` $where ORDER BY `contentid` $order LIMIT $offset,$pagesize");
			foreach($data as $r)
			{
				$html->show($r['contentid']);
			}
			if($pages > $page)
			{
				$page++;
				$creatednum = $offset + count($data);
				$percent = round($creatednum/$total, 2)*100;
				$message = "共需更新 <font color='red'>$total</font> 条信息<br />已完成 <font color='red'>{$creatednum}</font> 条（<font color='red'>{$percent}%</font>）";
				$forward = $start ? "?module=html&type=$type&dosubmit=1&first=$first&action=$action&fromid=$fromid&toid=$toid&fromdate=$fromdate&todate=$todate&pagesize=$pagesize&page=$page&pages=$pages&total=$total" : preg_replace("/&page=([0-9]+)&pages=([0-9]+)&total=([0-9]+)/", "&page=$page&pages=$pages&total=$total", URL);;
			}
			else
			{
				cache_delete('html_show_'.$_userid.'.php');
				$message = "更新完成！";
				$forward = '?module=html&action=show';
			}
			showmessage($message, $forward);
		}
		else
		{
			include ('html_show.tpl.php');
        }
		break;

    default :
}

?>